<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12" v-if="method != 'genter'">
            <el-form-item label="商家" prop="merchantId"
                :rules="[
                 ]">
                <GridSelect
                  title="选择商家"
                  labelName = 'merchantName'
                  labelValue = 'id'
                  :value = "inputForm.merchantId"
                  :limit="1"
                  @getValue='(value) => {inputForm.merchantId=value}'
                  :columns="[
                  {
                    prop: 'id',
                    label: '主键'
                  },
                  {
                    prop: 'merchantName',
                    label: '商家名称'
                  },
                  {
                    prop: 'storeAddress',
                    label: '商家地址'
                  }
                  ]"
                  :searchs="[
                  {
                    prop: 'merchantName',
                    label: '商家名称'
                  }
                  ]"
                  dataListUrl="/vmerchantinformation/vMerchantInformation/list"
                  entityBeanName="vmerchantinformation"
                  queryEntityUrl="/vmerchantinformation/vMerchantInformation/queryById">
              </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="logo"
              :rules="[]">
              <el-radio-group v-model="logos" @input="bztpChage">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
              </el-radio-group>
             </el-form-item>
        </el-col>
        <el-col :span="24" v-if="logos==1">
            <el-form-item label="标志图片" prop="logo"
                :rules="[
                  {required: true, message:'请上传logo', trigger:'blur'}
                 ]">
              <el-upload ref="logoPicture"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/vcodegenerationrecord/vCodeGenerationRecord`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                        $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                          dangerouslyUseHTMLString: true,
                          showConfirmButton: false,
                          closeOnClickModal: true,
                          customClass: 'showPic'
                        });
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.logo = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.logo = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="1"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="logoPictureArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="二维码色彩" prop="colour"
              :rules="[]">
              <el-radio-group v-model="inputForm.colour">
              <el-radio label="0">黑白</el-radio>
              <el-radio label="1">彩色</el-radio>
              </el-radio-group>
             </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="二维码数量" prop="quantity"
                :rules="[
                {required: true, message:'二维码张数不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.quantity" placeholder="请填写二维码张数"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="压缩密码"
              :rules="[]">
              <el-radio-group v-model="ysmm" @input="ysmmChage">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
              </el-radio-group>
             </el-form-item>
        </el-col>
        <el-col :span="12" v-if="ysmm==1">
            <el-form-item label="压缩密码" prop="packagePassword"
                :rules="[
                {required: true, message:'请填写压缩密码', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.packagePassword" type="password" placeholder="请填写压缩密码"></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        visible: false,
        loading: false,
        logoPictureArra: [],
        logos:'0',
        method:'',
        ysmm:'0',
        inputForm: {
          merchantId: '',
          logo:"",
          quantity:"",
          colour:"0",
          packagePassword:"",
        }
      } 
    },
    components: {
      GridSelect
    },
    methods: {
      ysmmChage(e){
        if(e==0){
          this.inputForm.packagePassword=""
        }
      },
      bztpChage(e){
        if(e==0){
          this.inputForm.logo=""
        }
      },
      init (method,id) {
        this.method = method
        this.inputForm.merchantId = id
        this.title = '生成二维码'
        this.logoPictureArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/vmerchantinformation/vMerchantInformation/generateQRCode`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>